package com.topboya.linker.diyview;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;
import com.zhy.view.flowlayout.TagAdapter;

/**
 * 演示一个变态需求的周视图
 * Created by huanghaibin on 2018/2/9.
 */

public class CustomWeekView extends WeekView {


    private int mRadius;

    /**
     * 自定义魅族标记的文本画笔
     */
    private Paint mTextPaint = new Paint();


    /**
     * 24节气画笔
     */
    private Paint mSolarTermTextPaint = new Paint();

    /**
     * 背景圆点
     */
    private Paint mPointPaint = new Paint();

    /**
     * 今天的背景色
     */
    private Paint mCurrentDayPaint = new Paint();


    /**
     * 圆点半径
     */
    private float mPointRadius;

    private int mPadding;

    private float mCircleRadius;
    /**
     * 自定义魅族标记的圆形背景
     */
    private Paint mSchemeBasicPaint = new Paint();

    private float mSchemeBaseLine;

    public CustomWeekView(Context context) {
        super(context);
        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);


        mSolarTermTextPaint.setColor(0xffff8148);
        mSolarTermTextPaint.setAntiAlias(true);
        mSolarTermTextPaint.setTextAlign(Paint.Align.CENTER);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setFakeBoldText(true);
        mSchemeBasicPaint.setColor(Color.WHITE);


        mCurrentDayPaint.setAntiAlias(true);
        mCurrentDayPaint.setStyle(Paint.Style.FILL);
        mCurrentDayPaint.setColor(0xFF05BA9A);

        mPointPaint.setAntiAlias(true);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setTextAlign(Paint.Align.CENTER);
        mPointPaint.setColor(Color.RED);

        mCircleRadius = dipToPx(getContext(), 7);

        mPadding = dipToPx(getContext(), 3);

        mPointRadius = dipToPx(context, 2);

        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mCircleRadius - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);

        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        //4.0以上硬件加速会导致无效
//        mSelectedPaint.setMaskFilter(new BlurMaskFilter(28, BlurMaskFilter.Blur.SOLID));

        setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemeBasicPaint);
//        mSchemeBasicPaint.setMaskFilter(new BlurMaskFilter(28, BlurMaskFilter.Blur.SOLID));

    }


    @Override
    protected void onPreviewHook() {
        mSolarTermTextPaint.setTextSize(mCurMonthLunarTextPaint.getTextSize());
        mRadius = Math.min(mItemWidth, mItemHeight) / 11 * 5;
    }


    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;

        if (calendar.isCurrentDay()){

            mSelectedPaint.setColor(0xFF05BA9A);
            mCurrentDayTextPaint.setAntiAlias(true);
            mCurrentDayTextPaint.setTextAlign(Paint.Align.CENTER);
            mSelectedPaint.setStyle(Paint.Style.FILL);

        }else {

            mSelectedPaint.setColor(0x80CFCFCF);

        }
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);




        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {

        boolean isSelected = isSelected(calendar);
        if (isSelected) {
            mPointPaint.setColor(Color.WHITE);
        } else {
            mPointPaint.setColor(Color.GRAY);
        }

        canvas.drawCircle(x + mItemWidth / 2, mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        int top = -mItemHeight / 6;


     LogUtils.v("day"+calendar.getDay()+"isSelected:"+isSelected+"calendar.isCurrentDay()"+calendar.isCurrentDay()+"hasScheme:"+hasScheme);

        if (isSelected ) {

            if (calendar.isCurrentDay()){
//onDrawSelected方法中設置背景，要不然今天有任务圆点不显示
//                mCurrentDayPaint.setStyle(Paint.Style.FILL);
//                mCurrentDayPaint.setColor(0xFF05BA9A);
//                canvas.drawCircle(cx, cy, mRadius, mCurrentDayPaint);

                mCurrentDayTextPaint.setColor(Color.WHITE);
                mCurrentDayLunarTextPaint.setColor(Color.WHITE);

                canvas.drawText("今", cx, mTextBaseLine + top,
                        mCurrentDayTextPaint);

                canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10,
                        mCurrentDayLunarTextPaint);

            }else {

                canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                        mSelectTextPaint);
                canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10, mSelectedLunarTextPaint);
//                canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10,
//                        calendar.isCurrentMonth() ? (!TextUtils.isEmpty(calendar.getSolarTerm())||!TextUtils.isEmpty(calendar.getGregorianFestival())||!TextUtils.isEmpty(calendar.getTraditionFestival())) ? mSolarTermTextPaint  :
//                                mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
            }

        } else if (hasScheme ) {

            if (calendar.isCurrentDay()) {

                mCurrentDayPaint.setStyle(Paint.Style.STROKE);
                mCurrentDayPaint.setColor(0xFF05BA9A);
                canvas.drawCircle(cx, cy, mRadius, mCurrentDayPaint);

            }
                canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                        calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

//                canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10,
//                        !TextUtils.isEmpty(calendar.getSolarTerm()) ? mSolarTermTextPaint : mSchemeLunarTextPaint);
            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10,
                    calendar.isCurrentMonth() ? (!TextUtils.isEmpty(calendar.getSolarTerm())||!TextUtils.isEmpty(calendar.getGregorianFestival())||!TextUtils.isEmpty(calendar.getTraditionFestival())) ? mSolarTermTextPaint  :
                            mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
        } else {
            if (calendar.isCurrentDay()) {

                mCurrentDayPaint.setStyle(Paint.Style.STROKE);
                mCurrentDayPaint.setColor(0xFF05BA9A);
                canvas.drawCircle(cx, cy, mRadius, mCurrentDayPaint);
            }
                canvas.drawText( String.valueOf(calendar.getDay()) , cx, mTextBaseLine + top,
                                calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);

                canvas.drawText(calendar.getLunar(), cx, mTextBaseLine  + mItemHeight / 10,
                                calendar.isCurrentMonth() ? (!TextUtils.isEmpty(calendar.getSolarTerm())||!TextUtils.isEmpty(calendar.getGregorianFestival())||!TextUtils.isEmpty(calendar.getTraditionFestival())) ? mSolarTermTextPaint  :
                                        mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);

        }

    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
